<template>
  <div class="alarmConfiguration zy-content">
    <!-- 搜索 -->
    <search :columns="searchData" @getSearch="getSearch(arguments)" key="search"></search>
    <div class="alarmConfiguration">
      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">设备信息</span>
          <Button @click="exportTemplateClick">
            {{
            $t("common.Export")
            }}
          </Button>
          <Button v-has="'reSort'" @click="reSort">重新排序</Button>
        </div>
        <!-- 表格 -->
        <div class="tableContent">
          <Table
            class="resizable-table"
            border
            @on-cell-click="clickDevice"
            @on-column-width-resize="(a,b,c,d)=>{onColumnWidthResize(a,b,c,d,'orderAdjustmentDeviceInfo')}"
            :columns="orderAdjustmentDeviceInfoList"
            ellipsis
            ref="selection"
            stripe
            :data="dataArr"
            highlight-row
            @on-selection-change="onSelectChange"
          ></Table>
        </div>
        <!-- 分页 -->
        <div class="pageWrap mt16">
          <pageLeftTitle :total="total" :selected="delIds.length" />
          <Page
            :total="total"
            :page-size="pageSize"
            :page-size-opts="pageSizeOption"
            :current="pageNum"
            @on-change="changePageNum"
            @on-page-size-change="changePageSize"
            show-sizer
            show-total
          />
        </div>
      </div>

      <!-- 工单信息 -->
      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">工单信息</span>
          <Button v-has="'orderAdjustment'" @click="orderAdjustment">工单置顶</Button>
          <Button v-has="'Replacingthemold'" @click="changeMold">更换工装</Button>
          <Button v-has="'Replacingthemachine'" @click="changeMachine">更换机台</Button>
        </div>
        <!-- {{ delIds1 }} -->
        <!-- 表格 -->
        <div class="tableContent">
          <!-- @on-drag-drop="drop" -->
          <Table
            class="resizable-table"
            border
            @on-column-width-resize="onColumnWidthResize"
            :columns="columnsList"
            ellipsis
            ref="selection"
            stripe
            :draggable="true"
            :data="dataArr1"
            @on-selection-change="onSelectChange1"
          ></Table>
        </div>
        <!-- 分页 -->
        <div class="pageWrap mt16">
          <pageLeftTitle :total="total1" :selected="delIds1.length" />
          <Page
            :total="total1"
            :page-size="pageSize1"
            :page-size-opts="pageSizeOption"
            :current="pageNum1"
            @on-change="changePageNum1"
            @on-page-size-change="changePageSize1"
            show-sizer
            show-total
          />
        </div>
      </div>
      <!-- 更换工装 -->
      <Modal
        :mask-closable="false"
        v-model="changeMoldShow"
        title="更换工装"
        class="modal-content-layout"
        width="55%"
      >
        <!-- {{ delIds2 }} -->
        <div class="alarmConfiguration">
          <div class="list-layout2">
            <div class="tableContent mt14">
              <Table
                class="resizable-table"
                border
                @on-column-width-resize="onColumnWidthResize"
                :columns="changeMoldColumns"
                ellipsis
                ref="selection"
                stripe
                :data="changeMoldData"
                @on-selection-change="onSelectChange2"
              ></Table>
            </div>
            <div class="pageWrap mt16">
              <pageLeftTitle :total="total2" :selected="changeMoldSelect.length" />
              <Page
                :total="total2"
                :page-size="pageSize2"
                :page-size-opts="pageSizeOption"
                :current="pageNum2"
                @on-change="changePageNum2"
                @on-page-size-change="changePageSize2"
                show-sizer
                show-total
              />
            </div>
          </div>
        </div>
        <div slot="footer">
          <Button type="default" @click="changeMoldShow = false">
            {{ $t("common.Cancle") }}
          </Button>
          <Button type="primary" :loading="loading" @click="linkMold">确定</Button>
        </div>
      </Modal>
      <!-- 转移机台 -->
      <Modal
        :mask-closable="false"
        v-model="changeMachineShow"
        title="转移机台"
        class="modal-content-layout"
        width="55%"
      >
        <search
          :columns="changeMachineSearch"
          @getSearch="getSearch2(arguments)"
          key="changeMachineSearch"
          :span="8"
        ></search>
        <!-- {{ delIds3 }} -->
        <div class="alarmConfiguration">
          <div class="list-layout2">
            <div class="tableContent mt14">
              <Table
                class="resizable-table"
                border
                @on-column-width-resize="onColumnWidthResize"
                :columns="changeMachineColumns"
                ellipsis
                ref="selection"
                stripe
                :data="changeMachineData"
                @on-selection-change="onSelectChange3"
              ></Table>
            </div>
            <div class="pageWrap mt16">
              <pageLeftTitle :total="total3" :selected="changeMachineSelect.length" />
              <Page
                :total="total3"
                :page-size="pageSize3"
                :page-size-opts="pageSizeOption"
                :current="pageNum3"
                @on-change="changePageNum3"
                @on-page-size-change="changePageSize3"
                show-sizer
                show-total
              />
            </div>
          </div>
        </div>
        <div slot="footer">
          <Button type="default" @click="changeMachineShow = false">
            {{ $t("common.Cancle") }}
          </Button>
          <Button type="primary" :loading="loading" @click="linkMachine">确定</Button>
        </div>
      </Modal>

      <importExcelModal
        :importExcelStateF="importExcelState"
        @importExcelChange="importExcelChange"
      ></importExcelModal>
    </div>
  </div>
</template>

<script>
import {
  getDeviceList,
  selectWorkOrderByDevice,
  getMoldList,
  replaceMold,
  // reSortPage,
  transferDevicePage,
  transferDevice,
  deleteApi,
  exportApi
} from "@/api/planManage/orderAdjustment.js";
import search from "@/components/curd/searchList.vue";
import importExcelModal from "@/components/importExcel.vue";
import fileDownload from "js-file-download";
import toggleTableOperation from "@/mixins/toggleTableOperation";
export default {
  name: "orderAdjustment",
  components: { importExcelModal, search },
  mixins: [toggleTableOperation],
  data() {
    return {
      searchData: [
        {
          title: "功能位置",
          key: "installPosition",
          type: "cascader",
          dicUrl: "/basic/organization/findListByToken?level=0",
          params: {
            label: "name",
            value: "code",
            children: "children"
          }
        },
        {
          title: "设备编号",
          key: "deviceNo"
        },
        {
          title: "是否排产",
          key: "isArranged",
          type: "select",
          dicData: [
            {
              label: "是",
              value: "是"
            },
            {
              label: "否",
              value: "否"
            }
          ]
        }
      ],
      changeMachineSearch: [
        {
          title: "设备编号",
          key: "deviceCode"
        },
        {
          title: "设备名称",
          key: "deviceName"
        }
      ], //转移机台弹窗搜索
      changeMachineShow: false, //转移机台弹窗
      changeMoldShow: false, //更换工装
      columns: [
        {
          type: "selection",
          width: 70,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "设备编号",
          key: "deviceNo",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "设备型号",
          key: "deviceSpec",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        }
      ],
      columns1: [
        {
          key: "selection",
          type: "selection",
          width: 60,
          resizable: true,
          align: "center"
        },
        {
          title: "生产顺序",
          key: "sort",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "工单号",
          key: "workCode",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "同组标识",
          key: "workOrderGroup",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "状态",
          key: "state",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "计划开始时间",
          key: "workStartTime",
          minWidth: 130,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "计划完成时间",
          key: "workEndTime",
          minWidth: 130,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "产品编号",
          key: "materialCode",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "产品名称",
          key: "materialName",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "产品型号",
          key: "materialModel",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "计划生产数量",
          key: "planProduceQuantity",
          minWidth: 130,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "生产数量",
          key: "produceQuantity",
          minWidth: 130,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "提报数量",
          key: "reportedQuantity",
          minWidth: 130,
          align: "center",
          resizable: true,
          tooltip: true
        },
        // {
        //   title: "工单状态",
        //   key: "state",
        //   minWidth: 130,
        //   align: "center",
        //   resizable: true,
        //   tooltip: true
        // },
        {
          title: "工装编号",
          key: "moldCode",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        }
      ],
      changeMachineColumns: [
        {
          key: "selection",
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "设备编号",
          key: "deviceNo",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "设备名称",
          key: "deviceName",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "设备状态",
          key: "deviceState",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        }
      ],
      changeMoldColumns: [
        {
          key: "selection",
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "工装编号",
          key: "moldCode",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "工装名称",
          key: "moldName",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "生产模数",
          key: "productionMode",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "工装状态",
          key: "moldState",
          minWidth: 100,
          align: "center",
          resizable: true,
          tooltip: true
        }
      ],
      moldStateList: [
        {
          label: "在用",
          value: "1"
        },
        {
          label: "报废",
          value: "2"
        },
        {
          label: "在库",
          value: "3"
        }
      ],
      disabled: false,
      backupList: [],
      shelfList: [],
      importExcelState: false /*批量导入*/,
      formItem: {},
      addtitle: "",
      allCheck: false,
      loading: false,
      create: false,
      searchParams: {},
      searchParams2: {},

      pageNum: 1,
      pageSize: 10,
      total: 0,
      dataArr: [],
      delIds: [],
      selectDeviceRow: {},

      pageNum1: 1,
      pageSize1: 10,
      total1: 0,
      dataArr1: [],
      delIds1: [],

      pageNum2: 1,
      pageSize2: 10,
      total2: 0,
      delIds2: [],

      pageNum3: 1,
      pageSize3: 10,
      total3: 0,
      delIds3: [],

      pageSizeOption: [10, 20, 30, 40],
      exportList: [],

      changeMachineData: [], //转换机台列表数据
      changeMachineSelect: [],
      changeMoldData: [],
      changeMoldSelect: [],
      selection: [],
      createByOptions: []
    };
  },
  mounted() {
    this.getTableColumnsWidth(this.columns1);
    this.getTableColumnsWidth(this.columns, "orderAdjustmentDeviceInfo");
    this.searchList();
  },
  methods: {
    getSearch(params) {
      this.searchParams = JSON.parse(JSON.stringify(params[0]));
      const ins = this.searchParams.installPosition;
      if (ins && ins.length > 0) {
        this.searchParams.installPosition = [ins.at(-1)];
      } else delete this.searchParams.installPosition;
      this.pageNum=1;
      this.searchList();
    },
    reset() {
      this.searchParams = {};
      this.changeNum();
    },
    //分页 设备信息
    changeNum(data) {
      this.pageNum = 1;
      this.searchParams = data;
      this.searchList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.searchList();
    },
    changePageNum(val) {
      this.pageNum = val;
      this.searchList();
    },
    // 工单信息
    changeNum1() {
      this.pageNum1 = 1;
      this.searchList1();
    },
    changePageSize1(pageSize) {
      this.pageSize1 = pageSize;
      this.searchList1();
    },
    changePageNum1(val) {
      this.pageNum1 = val;
      this.searchList1();
    },
    //更换工装
    changeNum2() {
      this.pageNum2 = 1;
      this.changeMold();
    },
    changePageSize2(pageSize) {
      this.pageSize2 = pageSize;
      this.changeMold();
    },
    changePageNum2(val) {
      this.pageNum2 = val;
      this.changeMold();
    },
    //更换机台
    changeNum3() {
      this.pageNum3 = 1;
      this.changeMachine();
    },
    changePageSize3(pageSize) {
      this.pageSize3 = pageSize;
      this.changeMachine();
    },
    changePageNum3(val) {
      this.pageNum3 = val;
      this.changeMachine();
    },
    //查询列表
    searchList() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      for (let key in this.searchParams) {
        if (this.searchParams[key]) {
          this.$set(params, key, this.searchParams[key]);
        }
      }
      getDeviceList(params).then(res => {
        if (res.code === 200) {
          this.dataArr = res.data.records;
          this.total = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    //点击工装查询工单
    clickDevice(row) {
      this.selectDeviceRow = row;
      this.searchList1();
    },
    //重新排序
    orderAdjustment() {
      if (this.delIds1.length !== 1) {
        this.$Message.warning("请选择一条工单");
        return;
      }
      let params = {
        deviceCode: this.delIds1[0].deviceCode,
        workCode: this.delIds1[0].workCode
      };
      this.axios({
        url: "/dms/productionWork/reSortWorkOrder",
        method: "put",
        params
      }).then(res => {
        if (res.code === 200) {
          this.$Message.success({
            content: res.msg,
            duration: 6,
            closable: true
          });
          this.searchList1();
        } else {
          this.$Message.error({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    reSort() {
      if (this.delIds.length !== 1) {
        this.$Message.warning("请选择一条工单");
        return;
      }
      let data = {
        deviceCode: this.delIds[0].deviceNo,
        // workCode: this.delIds1[0].workCode
      };
      this.axios({
        url: "/dms/productionWork/resetWork",
        method: "post",
        data
      }).then(res => {
        if (res.code === 200) {
          this.$Message.success({
            content: res.msg,
            duration: 6,
            closable: true
          });
          // this.searchList1();
        } else {
          this.$Message.error({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
      // reSortPage(params).then((res) => {
      //     if (res.code === 200) {
      //         this.dataArr1 = res.data.records;
      //         this.total1 = res.data.total;
      //     } else {
      //         this.$Message.warning({
      //             content: res.msg,
      //             duration: 6,
      //             closable: true,
      //         });
      //     }
      // });
    },
    //查询列表
    searchList1() {
      let params = {
        pageNum: this.pageNum1,
        pageSize: this.pageSize1,
        deviceCode: this.selectDeviceRow.deviceNo || this.de
      };
      selectWorkOrderByDevice(params).then(res => {
        if (res.code === 200) {
          this.dataArr1 = res.data.records;
          this.total1 = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    // currentRow(row) {
    //   getOrderList(row).then(res => {
    //     if (res.code === 200) {
    //       this.dataArr1 = res.data.records;
    //       this.total1 = res.data.total;
    //     } else {
    //       this.$Message.warning({
    //         content: res.msg,
    //         duration: 6,
    //         closable: true
    //       });
    //     }
    //   });
    // },
    //工单信息拖拽
    drop(first, end) {
      
      let oldList = this.dataArr1;
      let deleteItems = oldList.splice(first, 1)[0];
      oldList.splice(end, 0, deleteItems);
    },
    getSearch2(params) {
      this.searchParams2 = JSON.parse(JSON.stringify(params[0]));
      this.changeMachine();
    },
    //更换机台
    changeMachine() {
      if (this.delIds1.length !== 1) {
        this.$Message.warning({
          content: "只能选择一条数据",
          duration: 6,
          closable: true
        });
        return;
      }
      let params = {
        pageNum: this.pageNum3,
        pageSize: this.pageSize3,
        materialCode: this.delIds1[0].materialCode,
        ...this.searchParams2
      };
      transferDevicePage(params).then(res => {
        if (res.code == 200) {
          this.searchList1();
          this.changeMachineData = res.data.records;
          this.total3 = res.data.total;
          this.changeMachineShow = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    changeMold() {
      if (this.delIds1.length !== 1) {
        this.$Message.warning({
          content: "只能选择一条数据",
          duration: 6,
          closable: true
        });
        return;
      }
      let params = {
        pageNum: this.pageNum2,
        pageSize: this.pageSize2,
        materialCode: this.delIds1[0].materialCode,
        id: this.delIds1[0].id
      };
      getMoldList(params).then(res => {
        if (res.code === 200) {
          this.changeMoldData = res.data.records;
          this.total2 = res.data.total;
          this.changeMoldShow = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    //更换工装提交
    linkMold() {
      if (this.delIds2.length !== 1) {
        this.$Message.warning({
          content: "只能选择一条数据",
          duration: 6,
          closable: true
        });
        return;
      }
      let params = {
        moldCode: this.delIds2[0].moldCode,
        id: this.delIds1[0].id
      };
      replaceMold(params).then(res => {
        if (res.code === 200) {
          this.searchList1();
          this.$Message.success({
            content: res.msg,
            duration: 6,
            closable: true
          });
          this.changeMoldShow = false;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    linkMachine() {
      if (this.delIds3.length !== 1) {
        this.$Message.warning({
          content: "只能选择一条数据",
          duration: 6,
          closable: true
        });
        return;
      }
      let params = {
        deviceCode: this.delIds3[0].deviceNo,
        workStartTime: this.delIds1[0].workStartTime,
        workEndTime: this.delIds1[0].workEndTime,
        id: this.delIds1[0].id
      };
      transferDevice(params).then(res => {
        if (res.code === 200) {
          this.searchList1();
          this.$Message.success({
            content: res.msg,
            duration: 6,
            closable: true
          });
          this.changeMachineShow = false;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    check(row) {
      this.disabled = true;
      this.addtitle = `${this.$t("common.Check")}`;
      this.formItem = JSON.parse(JSON.stringify(row));
      this.create = true;
    },
    //勾选列表
    onSelectChange(selection) {
      let myArray = [];
      this.exportList = [];
      selection.map((item) => {
        myArray.push(item);
        this.exportList.push(item);
      });
      this.delIds = myArray;
      if (selection.length > 0) {
        this.allCheck = true;
      } else {
        this.allCheck = false;
      }
    },
    onSelectChange1(selection) {
      let myArray = [];
      selection.map((item, index) => {
        myArray.push(item);
      });
      this.delIds1 = myArray;
      if (selection.length > 0) {
        this.allCheck = true;
      } else {
        this.allCheck = false;
      }
    },
    onSelectChange2(selection) {
      let myArray = [];
      selection.map((item, index) => {
        myArray.push(item);
      });
      this.delIds2 = myArray;
    },
    onSelectChange3(selection) {
      let myArray = [];
      selection.map((item, index) => {
        myArray.push(item);
      });
      this.delIds3 = myArray;
    },
  
    //导出
    exportTemplateClick() {
      const filName = "工单信息";
      let params = this.exportList.map(item => item.deviceNo);
      exportApi(params).then(res => {
        fileDownload(res, filName + ".xlsx");
      });
    }
  }
};
</script>

<style scoped lang="scss">
.ivu-input-number {
  width: -webkit-fill-available;
}
</style>
